<template>
	<view class="pageview flex flex-direction">
		<zx-nav bg="#fff" title="好友代付"></zx-nav>
		<view class="page_title">来自{{ userName }}的代付</view>
		<view class="main flex-sub orderinfo">
			<view class="card">
				<view class="group-row flex align-center justify-between">
					<view class="title" style="width: 6em">订单信息</view>
					<view @tap="showDetails" class="flex align-center">
						查看详情
						<u-icon name="arrow-right" size="28"></u-icon>
					</view>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view>收件人</view>
					<view class="info">{{ orderInfo.contact_name || '' }}</view>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view>收货电话</view>
					<view class="info">{{ orderInfo.contact_phone || '' }}</view>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view>收货地址</view>
					<view class="flex-sub info">{{ orderInfo.address || '' }}</view>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view>下单时间</view>
					<view class="info">{{ orderInfo.created_at || '' }}</view>
				</view>

				<view class="group-row flex align-center justify-between u-m-t-20">
					<view>商品数量</view>
					<view class="info">{{ orderInfo.goods_qty || '' }}</view>
				</view>
			</view>

			<view class="card bg-white u-m-t-20">
				<view class="group-row flex align-center justify-between">
					<view class="title">商品费用</view>
					<text class="text-gray">￥{{ orderInfo.goods_amount || '' }}</text>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view class="title">运费</view>
					<text class="text-gray">￥{{ orderInfo.transport_amount || '' }}</text>
				</view>

				<view class="group-row flex align-center justify-between u-m-t-20">
					<view class="title">包装费</view>
					<text class="text-gray">￥{{ orderInfo.pack_amount || '' }}</text>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view class="title">订单金额</view>
					<text class="text-gray">￥{{ orderInfo.order_amount || '' }}</text>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view class="title">优惠劵</view>
					<text class="text-gray">￥{{ orderInfo.coupon_amount || '' }}</text>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view class="title">满减</view>
					<text class="text-gray">￥{{ orderInfo.deduction_amount || 0.0 }}</text>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20" v-if="orderInfo.balance_amount > 0">
					<view class="title">余额支付</view>
					<text class="text-gray">￥{{ orderInfo.balance_amount || '' }}</text>
				</view>
				<view class="group-row flex align-center justify-between u-m-t-20">
					<view class="title">支付金额</view>
					<text class="text-gray">￥{{ orderInfo.need_amount || '' }}</text>
				</view>
			</view>
		</view>
		<view class="footer fr u-m-l-50 u-m-r-50">
			<view class="btn" v-if="orderInfo.status == 1" @tap="orderConfirmPay">立即支付</view>
			<view class="text-gray" v-else>订单{{ orderInfo.status_ || '' }}</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			payway: 0,
			status: 4,
			orderInfo: {}, //订单信息
			goodsList: [],
			id: '',
			isShow: false,
			userName: '',
			userId: '',
			orderNo: '',
			type: 2 //列表跳转是2 ，支付付款是1
		};
	},
	onShow() {},
	onLoad(options) {
		this.orderNo = options.orderNo;
		this.userId = options.userId;
		this.type = options.type || 2;
		this.userName = options.userName;
		if (options.scene) {
			let scene = decodeURIComponent(options.scene).split('#');
			console.log('scene', scene);
			this.userId = scene[0];
			this.orderNo = scene[1];
			this.type = scene[2] || 2;
		}
		this.orderShow();
	},
	methods: {
		//查看订单
		orderShow() {
			let $me = this;
			$me.$postAction($me.$api.behalforderShow + this.orderNo + '/' + this.userId, {
				type: this.type
			}).then(da => {
				if (da.code == 0) {
					this.orderInfo = da.data;
					this.userName = this.orderInfo.contact_name;
				}
			});
		},

		//复制单号
		copyOrder(value) {
			uni.setClipboardData({
				data: value,
				success: function () {
					console.log('success');
				}
			});
		},
		//订单支付
		orderConfirmPay() {
			let $me = this;
			uni.login({
				provider: 'weixin',
				success: loginRes => {
					let param = {
						code: loginRes.code,
						is_balance: this.orderInfo.balance_amount > 0 ? 1 : 0,
						pay_type: 'wx_lite',
						is_payment_behalf: 1
					};
					console.log('$me.$api.orderPay',$me.$api.orderPay)
					$me.$postAction($me.$api.orderPay + '2/' + this.orderInfo.id, param).then(da => {
						console.log('确认支付' + JSON.stringify(da));
						if (da.code == 0) {
							let orderInfo = da.data;
							if (orderInfo) {
								this.payOrder(orderInfo);
							}
						}
					});
				}
			});
		},
		//订单支付
		payOrder(orderInfo) {
			let $me = this;
			uni.requestPayment({
				// #ifdef MP-WEIXIN
				provider: 'wxpay',
				timeStamp: orderInfo.timeStamp,
				nonceStr: orderInfo.nonceStr,
				package: orderInfo.package,
				signType: orderInfo.signType,
				paySign: orderInfo.paySign,
				service: 3,
				// #endif
				success: function (res) {
					uni.showModal({
						title: '支付提示',
						content: '您成功帮好友支付了订单',
						showCancel: false,
						confirmText: '返回首页',
						success: () => {
							uni.switchTab({
								url: '/pages/index/index'
							});
						}
					});
				},

				fail: function (err) {
					uni.showToast({
						icon: 'none',
						title: '订单支付失败'
					});
					setTimeout(function () {
						uni.switchTab({
							url: '/pages/index/index'
						});
					}, 800);
				}
			});
		},
		/* 查看详情 */
		showDetails() {
			uni.navigateTo({
				url: '/apppay/behalf/orderdetail?orderNo=' + this.orderNo + '&userId=' + this.userId + '&type=' + this.type
			});
		}
	}
};
</script>

<style lang="scss">
page,
.pageview {
	height: 100%;
	background: #ededed;
	overflow: hidden;
	width: 100%;
	color: #000;
	font-size: 26upx;
	.footer {
		padding-bottom: 30upx;
		padding-top: 30upx;
		text-align: center;
		.btn {
			font-size: 30upx;
			color: #fff;
			background: linear-gradient(90deg, #ff8173 0%, #ff3543 100%);
			box-shadow: 0px 12rpx 24rpx -9rpx #ffbec2;
			height: 80upx;
			line-height: 80upx;
			border-radius: 50upx;
			text-align: center;
			width: 100%;
		}
	}

	.main {
		overflow: auto;
		-webkit-overflow-scrolling: touch;
		padding: 0 0 30upx;
		height: 0;
		.card.comment {
			color: #333;
			font-size: 22upx;
			& > view + view {
				margin-top: 10upx;
			}
			span {
				font-size: 26upx;
				font-weight: bold;
			}
			.comment-info {
				color: #ff9933;
			}
		}

		.card {
			border-radius: 16upx;
			padding: 40upx 30upx;

			.group-row {
				color: #666;
				.title {
					width: 4em;
					margin-right: 40upx;
					font-weight: bold;
					color: #000;
				}

				.info {
					line-height: 30upx;
					padding: 10upx 0 10upx 30upx;
					color: #000000;
					text-align: right;
					&.flex-sub {
						width: 0;
					}
				}

				.text-gray {
					font-size: 28upx;
					color: #a4a4a4;
				}
			}
		}
	}
	.page_title {
		padding: 30upx;
		font-size: 32upx;
	}
}
</style>
